﻿Public Class frmChonMenu
    Dim frmCall As MainForm
    Dim bangMenu As New DataTable
    Public Sub New(ByVal frm As Form)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        frmCall = frm
    End Sub

    Private Sub frmChonMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'tao bang
        bangMenu.Columns.Add("Check", System.Type.GetType("System.Boolean"))
        bangMenu.Columns.Add("Menu_ID", System.Type.GetType("System.String"))
        bangMenu.Columns.Add("MenuName", System.Type.GetType("System.String"))
        bangMenu.Columns.Add("VBMenuName", System.Type.GetType("System.String"))
        Dim menu_id As Integer = 0
        Dim count As Integer = 0
        For Each menu As ToolStripMenuItem In frmCall.MenuStrip1.Items
            Try
                menu_id += 1
                count = 0
                For i As Integer = 0 To menu.DropDownItems.Count - 1
                    'Lenh Try Catch nay dung de chan menu.DropDownItems(i) la ToolStripSeparator
                    Try
                        Dim m As ToolStripMenuItem = menu.DropDownItems(i)
                        count += 1
                        Dim dong1 As DataRow = bangMenu.NewRow()
                        dong1(0) = False
                        If count < 10 Then
                            dong1(1) = menu_id.ToString() + "0" + count.ToString()
                        Else
                            dong1(1) = menu_id.ToString() + count.ToString()
                        End If
                        dong1(2) = m.Text
                        dong1(3) = m.Name
                        bangMenu.Rows.Add(dong1)
                        With gridDMMenu
                            .DataSource = bangMenu
                            .Columns.Item("Menu_ID").ReadOnly = True
                            .Columns.Item("MenuName").ReadOnly = True
                            .Columns.Item("VBMenuName").ReadOnly = True
                        End With
                    Catch ex As Exception

                    End Try
                Next
            Catch ex As Exception

            End Try
        Next
        'Try
        '    For Each toolStrip As ToolStripButton In frmCall.ToolStrip1.Items
        '        'Dim t As ToolStripMenuItem = toolStrip(0)
        '        Dim dong2 As DataRow = bangMenu.NewRow()
        '        dong2(0) = toolStrip.ToolTipText
        '        dong2(1) = toolStrip.Name
        '        bangMenu.Rows.Add(dong2)
        '        dongHienThi = toolStrip.ToolTipText & "-" & toolStrip.Name
        '        chkMenu.Items.Add(dongHienThi, False)
        '    Next
        'Catch ex As Exception
        'End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'lay tat ca cac menu khong duoc check va luu vao co so du lieu
        Dim sInsert As String = "delete from tblMenu"
        For Each dong As DataGridViewRow In gridDMMenu.Rows
            If dong.Cells("Check").Value Then
                sInsert &= " insert into tblMenu values(" & dong.Cells("Menu_ID").Value.ToString.Trim & ",N'" & dong.Cells("MenuName").Value.ToString.Trim & "',N'" & dong.Cells("VBMenuName").Value.ToString.Trim & "')"
            End If
        Next
        If (XL_DULIEU.ThucHienLenh(sInsert) = True) Then
            MessageBox.Show("Thành công")
        Else
            MessageBox.Show("Thất bại")
        End If
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkCheckAll.CheckedChanged
        For Each dr As DataGridViewRow In gridDMMenu.Rows
            If dr.Cells("MenuName").Value <> "Đăng nhập" And _
            dr.Cells("MenuName").Value <> "Đăng xuất" And _
            dr.Cells("MenuName").Value <> "&Thoát" Then
                dr.Cells("Check").Value = chkCheckAll.Checked
            End If
        Next
    End Sub
End Class